<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 2018/1/20
 * Time: 17:15
 */

/**
 * @var string $accountType
 * @var array $fields
 * @var $record \common\models\ar\RealName
 */

$str = "";
foreach ($fields as $v) {
    if ($v['is_must']) {
        $v['key'] = "<font color='red'> * </font>" . $v['key'];
    }

    $disabled = '';
    //已实名，只能操作可修改字段和末填写的不可修改记录
    if ($record && $record->status == 1 && $v['is_writable'] == 0 && $v['field_value'] != '') {
        $disabled = 'disabled';
    }
    
    $str .= /** @lang text */
        <<<EOF
<div class='form-group'>
    <label class="col-md-3 col-xs-12 control-label">{$v["key"]}</label>
    <div class="col-md-6 col-xs-12">
EOF;

    $name = $disabled == '' ? "name='f_" . $v['id'] . "'" : "";
    switch ($v['type']) {
        case 1://填空
            $str .= /** @lang text */
                <<<EOF
<div class="input-group">
    <span class="input-group-addon"><span class="fa fa-pencil"></span></span>
    <input $name 
           $disabled
           type="text"
           class="form-control"
           value="{$v['field_value']}"
           data-required="{$v['is_must']}"
           data-error={$v['placeholder']} >
</div>
<span class="help-block">{$v['placeholder']}</span>
EOF;
            break;
        case 3://单选
            $fieldTemplates = explode('|', $v['val']);//字段模板
            $selected = $v['field_value'] ? $v['field_value'] : 0;

            $radio = [];
            foreach ($fieldTemplates as $value) {
                $tmp = explode('=>', $value);
                $checked = $selected == $tmp[0] ? "checked='checked'" : '';

                $radio[] = "<label class='radio-inline'>
                                <input type='radio'
                                    $name
                                    $disabled
                                    $checked
                                    value='" . $tmp[0] . "'
                                    data-required='" . $v['is_must'] . "'
                                    data-error='" . $v['placeholder'] . "' />
                                " . $tmp[1] . "
                            </label>
                ";
            }

            $str .= implode(' ', $radio);
            $str .= "<span class='help-block'>" . $v['placeholder'] . "</span>";
            break;
        case 5://图片
            $pid="preview".$v['id'];
            $name_id="f_".$v['id'];
            $str.="<div id='$pid'></div>
             <br/>
            <input type='file' 
            oninput='previews(this,$pid)' 
            data-disabled='" . ($disabled != '' ? 1 : 0) . "' 
            data-required='" . $v['is_must'] . "'
            value='" . $v['field_value'] . "'
            name='" .$name_id. "'
            data-error='" . $v['placeholder'] . "'
            class='form-control'/>";
            break;//            name='money_order'
        case 7://选择框
            $fieldTemplates = explode('|', $v['val']);//字段模板
            $selected = $v['field_value'] ? $v['field_value'] : 0;
            $str .= "<select 
                        $name 
                        $disabled
                        class='form-control' 
                        data-required='" . $v['is_must'] . "' 
                        data-error='" . $v['placeholder'] . "'>";
            foreach ($fieldTemplates as $value) {
                $tmp = explode('=>', $value);
                $str .= "<option value='" . $tmp[0] . "' " . ($tmp[0] == $selected ? "selected" : "") . ">" . $tmp[1] . "</option>";
            }
            $str .= "</select>";
            $str .= "<span class='help-block'>" . $v['placeholder'] . "</span>";
            break;
        default:
            break;
    }
    $str .= "</div></div>";
}
?>
<div class="row">
    <div class="col-md-12">
        <form class="form-horizontal" id="form_realNameAuth">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">实名验证</h3>
                </div>
                <div class="panel-body">
                    <?php
                    $alertClass = '';
                    $msg = '';
                    if ($record) {
                        //有新的提交
                        if ($record->update_status != 0) {
                            switch ($record->update_status) {
                                case 1:
                                    $alertClass = "info";
                                    $msg = "新提交的实名资料正在审核中，请耐心等待";
                                    break;
                                case 2:
                                    //理论上不会到达这里，当后台审核成功后，会将update_status置为初始0
                                    $alertClass = "success";
                                    $msg = "实名资料已通过审核";
                                    break;
                                case 3:
                                    $alertClass = "danger";
                                    $msg = "新提交的实名资料审核失败：" . $record->update_error_msg;
                                    break;
                            }
                        } else {
                            //没有新提交
                            if ($record->status == 1) {
                                if ($record->progress == 1) {
                                    $alertClass = "success";
                                    $msg = "实名资料已通过审核";
                                } else {
                                    $alertClass = "danger";
                                    $msg = "请完善您的实名资料";
                                }
                            }
                        }
                    } else {
                        $alertClass = "danger";
                        $msg = "请填写并上传您的实名资料";
                    }
                    ?>
                    <div class="alert alert-<?= $alertClass; ?>" role="alert">
                        <i class="fa fa-bell-o" style="margin-right:10px;"></i> <?= $msg; ?>
                    </div>
                </div>
                <div class="panel-body form-group-separated">
                    <?= $str; ?>
                    <div class="form-group"></div>
                    <div class="panel-footer">
                        <button type='button' class="btn btn-info pull-right btn-block" onclick="RealNamesObj.realNameAuth()">
                            提 交
                        </button>
                    </div>
                </div>
                <?php
                echo \yii\helpers\Html::hiddenInput(
                    Yii::$app->request->csrfParam,
                    Yii::$app->request->getCsrfToken()
                );
                ?>
            </div>
        </form>
    </div>
</div>
<!--<script type="text/javascript" src="/plugins/atlant/js/fileinput/fileinput.min.js"></script>-->
<script src="/js/RealNames.js?v=<?= Yii::$app->params['version']; ?>"></script>
<script type="text/javascript">
    function previews(file,pid) {
        var prevDiv = document.getElementById(pid.id);
        if (file.files && file.files[0]) {
            var reader = new FileReader();
            reader.onload = function(evt) {
                prevDiv.innerHTML = '<img style="width: 300px;height: 200px;" src="' + evt.target.result + '" />\n';
            }
            reader.readAsDataURL(file.files[0]);
        } else {
            prevDiv.innerHTML = '<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + file.value + '\'"></div>';
        }
    }
</script>